#!/bin/bash

# shellcheck disable=SC2120

# Provide source directive to shellcheck.
# shellcheck source=meta-facebook/meta-yosemite4/recipes-phosphor/state/phosphor-state-manager/power-cmd
source /usr/libexec/phosphor-state-manager/power-cmd
# shellcheck source=meta-facebook/meta-yosemite4/recipes-yosemite4/plat-tool/files/yosemite4-common-functions
source /usr/libexec/yosemite4-common-functions

#IO 0:7 input port for showing slot 1:8 power status
#IO 8:16 output port for controlling slot 1:8 power status
CHASSIS_ID=$1
IO_EXP_SLOT_PWR_STATUS=$((CHASSIS_ID - 1))
IO_EXP_SLOT_PWR_CTRL=$((IO_EXP_SLOT_PWR_STATUS + 8))
IO_EXP_SLED_CYCLE=12
#IO_EXP_BIC_PWR_CTRL=32
IO_EXP_SLOT_12V_STATUS=$((CHASSIS_ID + 23))
MANAGEMENT_BOARD_VERSION=$(get_product_version Management_Board)

GPIOCHIP_IO_EXP_SLOT_PWR_CTRL=$(basename "/sys/bus/i2c/devices/$SPIDER_BOARD_IO_EXP_BUS_NUM-00$IO_EXP_SLOT_PWR_CTRL_ADDR/"*gpiochip*)
if [ -z "$MANAGEMENT_BOARD_VERSION" ]; then
    echo "Failed to check management board fru info, sled cycle keep default setting"
elif [ "$MANAGEMENT_BOARD_VERSION" = "DVT" ] || [ "$MANAGEMENT_BOARD_VERSION" = "EVT" ]; then
    GPIOCHIP_IO_EXP_SLED_PWR_CTRL=$(basename "/sys/bus/i2c/devices/$MANAGEMENT_BOARD_IO_EXP_BUS_NUM-00$IO_EXP_SLED_PWR_CTRL_ADDR/"*gpiochip*)
fi

#GPIOCHIP_IO_EXP_BIC_PWR_CTRL=$(basename "/sys/bus/i2c/devices/$IO_EXP_SLOT_PWR_STATUS-00$IO_EXP_BIC_PWR_CTRL_ADDR/"*gpiochip*)

is_nuvoton_board="$(check_nuvoton_board)"

chassis-power-cycle()
{
    CHASSIS_ID=$1

    if [ "$CHASSIS_ID" -le 8  ]
    then
        chassis_status=$(gpio_get "$GPIOCHIP_IO_EXP_SLOT_PWR_CTRL" "$IO_EXP_SLOT_PWR_STATUS")
        if [ "$chassis_status" == "$STATE_ON" ]
        then
            if [ -n "$is_nuvoton_board" ]
            then
                # inform management CPLD that the slot is 12V off
                if ! gpio_set "$GPIOCHIP_IO_EXP_SLED_PWR_CTRL" "$IO_EXP_SLOT_12V_STATUS"=0
                then
                    echo "Failed to inform management CPLD that slot$1 is 12V off"
                fi
            fi
            sleep 1
            if ! gpio_set "$GPIOCHIP_IO_EXP_SLOT_PWR_CTRL" "$IO_EXP_SLOT_PWR_CTRL"=1
            then
                msg="Failed to set slot$CHASSIS_ID power off"
                echo "${msg}"
                add_sel "${msg}"
            fi
            sleep 20
        fi
        if ! gpio_set "$GPIOCHIP_IO_EXP_SLOT_PWR_CTRL" "$IO_EXP_SLOT_PWR_CTRL"=0
        then
            msg="Failed to set slot$CHASSIS_ID power on"
            echo "${msg}"
            add_sel "${msg}"
        fi
        sleep 2

        # Check chassis status after doing 12V cycle
        chassis_status=$(get_ac_on_status "$GPIOCHIP_IO_EXP_SLOT_PWR_CTRL" "$IO_EXP_SLOT_PWR_STATUS")
        if [ "$chassis_status" == "$STATE_ON" ]
        then
            if [ -n "$is_nuvoton_board" ]
            then
                # inform management CPLD that the slot is 12V on
                if ! gpio_set "$GPIOCHIP_IO_EXP_SLED_PWR_CTRL" "$IO_EXP_SLOT_12V_STATUS"=1
                then
                    echo "Failed to inform management CPLD that slot$1 is 12V on"
                fi
            fi
            busctl set-property "$CHASSIS_BUS_NAME""$CHASSIS_ID" "$CHASSIS_OBJ_PATH""$CHASSIS_ID" "$CHASSIS_INTF_NAME" "$CHASSIS_PROPERTY_NAME" s "$CHASSIS_ON_PROPERTY"
            /usr/libexec/phosphor-state-manager/wait-until-mctp-connection-done "$CHASSIS_ID" && systemctl restart "phosphor-discover-system-state@$CHASSIS_ID.service"
            msg="Chassis$CHASSIS_ID cycle success"
            echo "${msg}"
            add_sel "${msg}"
            exit 0;
        else
            busctl set-property "$CHASSIS_BUS_NAME""$CHASSIS_ID" "$CHASSIS_OBJ_PATH""$CHASSIS_ID" "$CHASSIS_INTF_NAME" "$CHASSIS_PROPERTY_NAME" s "$CHASSIS_OFF_PROPERTY"
            msg="Chassis$CHASSIS_ID cycle failed, Chassis$CHASSIS_ID is power off"
            echo "${msg}"
            add_sel "${msg}"
            exit 1;
        fi
    fi

    echo "Invalid slot id"
    exit 1;
}

sled-cycle()
{
    if [ -z "$MANAGEMENT_BOARD_VERSION" ]; then
        set_gpio FM_BMC_SLED_CYCLE_R 1
    elif [ "$MANAGEMENT_BOARD_VERSION" = "DVT" ] || [ "$MANAGEMENT_BOARD_VERSION" = "EVT" ]; then
        gpio_set "$GPIOCHIP_IO_EXP_SLED_PWR_CTRL" "$IO_EXP_SLED_CYCLE"=1
    else
        set_gpio FM_BMC_SLED_CYCLE_R 1
    fi
}

if [ "$1" == 0 ]
then
    echo "Starting sled cycle..."
    if ! sled-cycle
    then
        echo "Failed to do sled cycle"
    fi
else
    echo "Starting slot$1 cycle"
    chassis-power-cycle "$1"
fi
